import static CommonUtil.*

class CustomerController {

    def searchableService

    def index = { redirect(action: list, params: params) }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']

    def list = {
        if (!params.max) params.max = 10
        [customerInstanceList: Customer.list(params), total:Customer.count()]
    }

    def show = {
        def customerInstance = Customer.get(params.id)
        if (!customerInstance) {
            flash.message = message(code: 'customer.controller.notfound', args: [params.id])
            redirect(action: list)
        }
        else {
            def jobs = customerInstance.jobs.sort {
                it.id
            }
            AuthUser authUser = AuthUser.findByPerson(customerInstance)
            return [customerInstance: customerInstance, authUser: authUser, jobs: jobs]
        }
    }

    def delete = {
        def customerInstance = Customer.get(params.id)
        if (customerInstance) {
            customerInstance.delete()
            flash.message = "Customer ${params.id} deleted"
            redirect(action: list)
        }
        else {
            flash.message = message(code: 'customer.controller.notfound', args: [params.id])
            redirect(action: list)
        }
    }

    def edit = {
        def customerInstance = Customer.get(params.id)

        if (!customerInstance) {
            flash.message = message(code: 'customer.controller.notfound', args: [params.id])
            redirect(action: list)
        }
        else {
            return [customerInstance: customerInstance]
        }
    }

    def update = {
        def customerInstance = Customer.get(params.id)
        if (customerInstance) {
            customerInstance.properties = params
            if (!customerInstance.hasErrors() && customerInstance.save()) {
                flash.message = "Customer ${params.id} updated"
                redirect(action: show, id: customerInstance.id)
            }
            else {
                render(view: 'edit', model: [customerInstance: customerInstance])
            }
        }
        else {
            flash.message = message(code: 'customer.controller.notfound', args: [params.id])
            redirect(action: edit, id: params.id)
        }
    }

    def create = {
        def customerInstance = new Customer()
        customerInstance.properties = params
        return ['customerInstance': customerInstance]
    }

    def save = {
        def customerInstance = new Customer(params)
        if (!customerInstance.hasErrors() && customerInstance.save()) {
            flash.message = "Customer ${customerInstance.id} created"
            redirect(action: show, id: customerInstance.id)
        }
        else {
            render(view: 'create', model: [customerInstance: customerInstance])
        }
    }

    def search = {
        boolean hasQuery = !isEmpty(params.query)
        if (hasQuery) {
            final def searchReturn = Customer.search(params.query, params)
            def customerInstanceList = searchReturn?.results
            if (customerInstanceList?.size() == 0) {
                flash.message = message(code: 'common.search.notfound')
            }
            return [customerInstanceList: customerInstanceList,
                    total:searchReturn?.total,
                    lastQuery:params.query]
        }
    }

    /*
    web flow -- new job for customer
    1. customer come to shop and select photo to print or other products and services
    2. shop assitant help customer to open a new Job in system, if the customer is a member, use the memebership; otherwise use non-member customer to open new Job
    3. shop assitant add job item according to the product selection, system display the total value of the Job and value for each job item
    4. shop assitant click job print button to print photo collection ticket
    5. customer use collection ticket to collect photo or products, shop assitant update job status
     */
    /*def newJobFlow ={
        search{
            
        }

    }*/
}
